local jixiang = fk.CreateSkill {
  name = "jixiang",
}
local function initializeAllCardNames(player, mark)
  if type(player:getMark(mark)) == "table" then
    return player:getMark(mark)
  end
  local names = {}
  for _, id in ipairs(Fk:getAllCardIds()) do
    local card = Fk:getCardById(id)
    if card.type == Card.TypeBasic and not card.is_derived then
      table.insertIfNeed(names, card.name)
    end
  end
  player.room:setPlayerMark(player, mark, names)
  return names
end

local func={
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase ~= Player.NotActive and player ~= target and data.pattern then
      local names = initializeAllCardNames(player, "jixiang_names")
      local mark = player:getMark("jixiang-turn")
      for _, name in ipairs(names) do
        local card = Fk:cloneCard(name)
        if (type(mark) ~= "table" or not table.contains(mark, card.trueName)) and Exppattern:Parse(data.pattern):match(card) then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#jixiang-invoke::"..target.id, true)
    if #card > 0 then
      self.cost_data = card
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data, self.name, player, player)
    local names = initializeAllCardNames(player, "jixiang_names")
    local names2 = {}
    local mark = player:getMark("jixiang-turn")
    for _, name in ipairs(names) do
      local card = Fk:cloneCard(name)
      if (type(mark) ~= "table" or not table.contains(mark, card.trueName)) and Exppattern:Parse(data.pattern):match(card) then
        table.insertIfNeed(names2, name)
      end
    end
    if #names2 == 0 then return false end
    if event == fk.AskForCardUse then
      local extra_data = data.extraData
      local isAvailableTarget = function(card, p)
        if extra_data then
          if type(extra_data.must_targets) == "table" and #extra_data.must_targets > 0 and
              not table.contains(extra_data.must_targets, p.id) then
            return false
          end
          if type(extra_data.exclusive_targets) == "table" and #extra_data.exclusive_targets > 0 and
              not table.contains(extra_data.exclusive_targets, p.id) then
            return false
          end
        end
        return not target:isProhibited(p, card) and card.skill:modTargetFilter(p.id, {}, target, card, true)
      end
      local findCardTarget = function(card)
        local tos = {}
        for _, p in ipairs(room.alive_players) do
          if isAvailableTarget(card, p) then
            table.insert(tos, p.id)
          end
        end
        return tos
      end
      names2 = table.filter(names2, function (c_name)
        local card = Fk:cloneCard(c_name)
        return not target:prohibitUse(card) and (card.skill:getMinTargetNum() == 0 or #findCardTarget(card) > 0)
      end)
      if #names2 == 0 then return false end
      local name = room:askForChoice(player, names2, self.name, "#jixiang-name::" .. target.id, false, names)
      local card = Fk:cloneCard(name)
      card.skillName = self.name
      data.result = {
        from = target.id,
        card = card,
      }
      if card.skill:getMinTargetNum() == 1 then
        local tos = findCardTarget(card)
        if #tos > 0 then
          data.result.tos = {room:askForChoosePlayers(target, tos, 1, 1, "#jixiang-target:::" .. name, self.name, false, true)}
        else
          return false
        end
      end
      if data.eventData then
        data.result.toCard = data.eventData.toCard
        data.result.responseToEvent = data.eventData.responseToEvent
      end
      local mark = player:getMark("jixiang-turn")
      if type(mark) ~= "table" then mark = {} end
      table.insert(mark, card.trueName)
      room:setPlayerMark(player, "jixiang-turn", mark)
      return true
    else
      names2 = table.filter(names2, function (c_name)
        return not target:prohibitResponse(Fk:cloneCard(c_name))
      end)
      if #names2 == 0 then return false end
      local name = room:askForChoice(player, names2, self.name, "#jixiang-name::" .. target.id, false, names)
      local card = Fk:cloneCard(name)
      card.skillName = self.name
      data.result = card
      local mark = player:getMark("jixiang-turn")
      if type(mark) ~= "table" then mark = {} end
      table.insert(mark, card.trueName)
      room:setPlayerMark(player, "jixiang-turn", mark)
      return true
    end
  end
}

local func2={
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and player.phase ~= Player.NotActive and table.contains(data.card.skillNames, jixiang.name)
  end,
  on_cost = function() return true end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, jixiang.name)
    player.room:addPlayerMark(player, "chengxian_extratimes-phase")
  end,
}
jixiang:addEffect(fk.AskForCardUse,  func)
jixiang:addEffect(fk.AskForCardResponse,  func)
jixiang:addEffect(fk.CardUseFinished,  func2)
jixiang:addEffect(fk.CardRespondFinished,  func2)

return jixiang